$(document).ready(function() {
	$("#signupBut").click(function() {
		return $.formValidator.pageIsValid("2");
	});
	
	$("#clause2").click(function() {
		if($("#clause2").prop("checked") == true) {
			revert();
		} else {
			unClause();
		}
	});
	
	$.formValidator.initConfig({
		validatorGroup:"2",
		onSuccess: function() {
			clickSignupBtn();
			var loginName = $("#loginName2").val();
			var nickName = $("#nickName2").val();
			var passWord = $("#passWord2").val();
			var verifyCode = $("#verifyCode2").val();
			
			var jsonStr = {"loginName":loginName, "nickName":nickName, "passWord":passWord, "vcode":verifyCode};
			jQuery.ajax({
				async: false,
				type:"post", 
				dataType: "json",
				contentType:"application/json; charset=utf-8",
				url:"/doSignup",
				data:JSON.stringify(jsonStr),
				success: function(data, textStatus) {
					var code = data.code;
					switch (code) {
				    case 10001: 
				    	$("#signupMsg").html('请填写完整的用户信息！');
				    	break;
				    case 10003: 
				    	$("#signupMsg").html('验证码错误， 请重新输入！');
				    	break;
				    case 10006: 
				    	$("#signupMsg").html('对不起, 用户名已存在！');
				    	break;
				    case 10000: 
				    	location.href = "/signupSuc";
				    	break;
					}
					revert();
		      	},
				error : function (XMLHttpRequest, textStatus, errorThrown) {
				    alert("小囧囧，出错了哦， 请联系管理员！！！");
				    revert();
				}
			});
		}
	});
	
	$("#loginName2").formValidator({
		validatorGroup:"2",
		onShow: "请输入用户名",
		onFocus: "格式：jsbl@jiong.com",
		onCorrect: "该邮箱可以注册"
	}).regexValidator({
		regExp: "email",
		dataType: "enum",
		onError: "邮箱格式不正确"
	}).ajaxValidator({
		async: false,
		type:"post",
		dataType : "json",
		contentType:"application/json; charset=utf-8",
		url:"/isUserExist",
		data:{"loginName":function(){return $("#loginName2").val()}},
		success : function(data){
			var code = data.code;
			if(code == 10000) {
				return true;
			} else if(code == 10006) {
		    	return false;
			}
			return false;
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
		    alert("小囧囧，出错了哦， 请联系管理员！！！");
		},
		onError : "该邮箱已注册， 请更换！",
		onWait : "正在校验邮箱，请稍候..."
	});
	
	$("#nickName2").formValidator({
		validatorGroup:"2",
		onShow: "请输入昵称",
		onFocus: "只允许输入数字、字母、汉字",
		onCorrect: "输入正确"
	}).inputValidator({
		min: 1,
		empty: {
			leftEmpty: false,
			rightEmpty: false,
			emptyError: "请输入昵称"
		},
		onError: "数字、字母、汉字的任意组合"
	}).regexValidator({
		regExp: "letterAndNoAndHanzi",
		dataType: "enum",
		onError: "只能输入字母、汉字"
	});
	
	$("#passWord2").formValidator({
		validatorGroup:"2",
		onShow: "请输入密码",
		onFocus: "6-18位字母、数字、符号",
		onCorrect: "密码格式正确"
	}).inputValidator({
		min: 6,
		max: 18,
		empty: {
			leftEmpty: false,
			rightEmpty: false,
			emptyError: "请输入密码"
		},
		onError: "6-18位字母、数字、符号的任意组合"
	});
	
	$("#rePwd2").formValidator({
		validatorGroup:"2",
		onShow: "请确认密码",
		onFocus: "请再次输入密码",
		onCorrect: "密码一致"
	}).inputValidator({
		min: 6,
		max: 18,
		empty: {
			leftEmpty: false,
			rightEmpty: false,
			emptyError: "请再次输入密码"
		},
		onError: "两次密码必须保持一致"
	}).compareValidator({
		desID: "passWord2",
		operateor: "=",
		onError: "两次密码不一致, 请确认"
	});
	
	$("#verifyCode2").formValidator({
		validatorGroup:"2",
		onShow: "请输入验证码",
		onFocus: "4位字母或数字的组合",
		onCorrect: "验证码格式正确"
	}).inputValidator({
		min: 4,
		max: 4,
		empty: {
			leftEmpty: false,
			rightEmpty: false,
			emptyError: "请输入验证码"
		},
		onError: "验证码为4位字母或数字"
	}).regexValidator({
		regExp: "letterAndNO",
		dataType: "enum",
		onError: "只能输入字母或数字"
	});
	
});

function clickSignupBtn() {
	$("#signupBut").prop("disabled", true);
	$("#signupBut").addClass("disabledBtn");
	$("#signupBut").val("正在请求...");
}

function unClause() {
	$("#signupBut").prop("disabled", true);
	$("#signupBut").addClass("disabledBtn");
	$("#signupBut").val("未同意条款");
}

function revert() {
	$("#signupBut").prop("disabled", false);
	$("#signupBut").removeClass("disabledBtn");
	$("#signupBut").val(" 走  你 ");
}